-- ====================================================
-- by:姜
-- 源码来源 Lan宝  简单修改 方便添加多个容器 和不同布局
-- 使用粘贴即可 修改容器配置
-- 附一份配置参考
-- params.wanyuan_backpack =
-- {
--     widget =
--     {
--         slotpos = {},
--         animbank = "ui_wanyuan_backpack",
--         animbuild = "ui_wanyuan_backpack",
--         pos = Vector3(-18, -100, 0),
--         -- 翻页配置
--         enable_Page = true,
--         page_grid_num = 18,
--         max_page = 3,
--         page_text_x = -167 / 2 - 40,
--         page_text_y = -360,
--         page_btn_x = -167,
--         page_btn_spacing = 75,
--         page_btn_y = -360,
--         page_btn_x_scale = .7,
--         page_btn_y_scale = .7,

--     },
--     usespecificslotsforitems = true,  -- 这个得有
--     issidewidget = true,
--     type = "pack",
--     openlimit = 1,
--     itemtestfn = function(container, item, slot)
--         return not item.replica.container
--     end,
-- }

-- --------------------------------------------------------------------
local ImageButton = require "widgets/imagebutton"
local Text = require "widgets/text"
-- 唯一标识
local unique_id = "wanyuan_"
-- 更新页面显示的函数
local function UpdatePageDisplay(self, container, widget)
    for i = 1, widget.max_page * widget.page_grid_num do
        if math.ceil(i / widget.page_grid_num) == container.cur_page then
            self.inv[i]:Show()
        else
            self.inv[i]:Hide()
        end
    end
    self[unique_id .. "page_text"]:SetString(container.cur_page .. '/' .. widget.max_page)
end

-- 创建翻页按钮的函数
local function CreatePageButton(self, container, widget, direction)
    local button = self:AddChild(ImageButton(
        'images/global_redux.xml',
        'arrow2_' .. direction .. '.tex',
        'arrow2_' .. direction .. '.tex',
        'arrow2_' .. direction .. '.tex',
        'arrow2_' .. direction .. '.tex',
        'arrow2_' .. direction .. '.tex'
    ))

    local x_offset = direction == "right" and widget.page_btn_spacing or 0
    button:SetPosition(widget.page_btn_x + x_offset, widget.page_btn_y)
    button:SetScale(widget.page_btn_x_scale, widget.page_btn_y_scale)

    button:SetOnClick(function()
        local page_change = direction == "right" and 1 or -1
        container.cur_page = math.clamp(container.cur_page + page_change, 1, widget.max_page)
        UpdatePageDisplay(self, container, widget)
        SendModRPCToServer(GetModRPC(unique_id .. "Page_Button", unique_id .. "container_switch_page"),
            container.cur_page, container)
    end)

    return button
end

-- 获取物品槽位的函数
local function GetItemSlot(item, container, widget, cur_page)
    if not item or not widget.enable_Page then return nil end

    local free_slot = nil
    for i = 1, container:GetNumSlots() do
        if math.ceil(i / widget.page_grid_num) == cur_page then
            local item_in_slot = container:GetItemInSlot(i)

            if item_in_slot then
                if item_in_slot.prefab == item.prefab and
                    item_in_slot.replica.stackable and
                    not item_in_slot.replica.stackable:IsFull() then
                    return i
                end
            else
                free_slot = free_slot and math.min(free_slot, i) or i
            end
        end
    end
    return free_slot
end

-- 修改 ContainerWidget
AddClassPostConstruct('widgets/containerwidget', function(self, ...)
    local old_Open = self.Open

    function self:Open(container, doer, ...)
        if old_Open then
            old_Open(self, container, doer, ...)
        end

        local widget = container.replica.container:GetWidget()
        if not (container and widget and widget.enable_Page) then return end

        if not self[unique_id .. "page_btn_enable"] then
            self[unique_id .. "page_btn_enable"] = true
            container.cur_page = container.cur_page or 1

            -- 创建页码文本
            self[unique_id .. "page_text"] = self:AddChild(Text(NUMBERFONT, 35,
                container.cur_page .. '/' .. widget.max_page))
            self[unique_id .. "page_text"]:SetPosition(widget.page_text_x, widget.page_text_y)

            -- 创建翻页按钮
            self[unique_id .. "page_btn_right"] = CreatePageButton(self, container, widget, "right")
            self[unique_id .. "page_btn_left"] = CreatePageButton(self, container, widget, "left")

            -- 初始化显示
            UpdatePageDisplay(self, container, widget)
        end
    end
end)

-- 修改 ContainerReplica
AddClassPostConstruct('components/container_replica', function(self, ...)
    local old_GetSpecificSlotForItem = self.GetSpecificSlotForItem

    function self:GetSpecificSlotForItem(item, ...)
        if not self.usespecificslotsforitems then
            return old_GetSpecificSlotForItem and old_GetSpecificSlotForItem(self, item, ...)
        end
        local widget = self:GetWidget()
        return GetItemSlot(item, self, widget, self.inst.cur_page or 1) or
            (old_GetSpecificSlotForItem and old_GetSpecificSlotForItem(self, item, ...))
    end
end)

-- 修改 Container 组件
AddComponentPostInit('container', function(self)
    local old_GetSpecificSlotForItem = self.GetSpecificSlotForItem

    function self:GetSpecificSlotForItem(item, ...)
        if not self.usespecificslotsforitems then
            return old_GetSpecificSlotForItem and old_GetSpecificSlotForItem(self, item, ...)
        end

        local widget = self:GetWidget()
        return GetItemSlot(item, self, widget, self.inst.cur_page or 1) or
            (old_GetSpecificSlotForItem and old_GetSpecificSlotForItem(self, item, ...))
    end
end)
-- RPC 处理器
local unique_id = "wanyuan_"
AddModRPCHandler(unique_id .. "Page_Button", unique_id .. "container_switch_page", function(player, page, container)
    if container and page then
        container.cur_page = page
    end
end)
